package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.UserDao;
import com.cn.wanxi.dao.impl.UserImpl;
import com.cn.wanxi.model.ResultModel;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.service.UserService;

import java.util.List;

/**
 * 查询用model和list来接收
 * 新增，修改，删除用int接收
 */
public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserImpl();

    @Override
    public ResultModel login(UserModel userModel) {
//        ResultModel resultModel = new ResultModel();
        String code = userModel.getCode();
        String result = "";
        if (code.toUpperCase().equals(userModel.getSessionCode())) {
//    表示验证码正确
//            需要查询数据库，根据用户名和密码判断是否正确
//            需要数据实现层（dao）查询数据

            UserModel model = userDao.findByUsernameAndPassword(userModel);
//            只要查询出来的数据不等于null，表示用户名和密码可以相互匹配
//            否则表示用户名或者密码错误
            boolean isHave = model != null;
            if (isHave) {
                result = "success";
            } else {
                result = "error";
            }

        } else {
//            表示验证码错误
            result = "error-code";
        }
        return ResultModel.getModel(result);
    }

    @Override
    public ResultModel findAll(UserModel userModel) {
//        ResultModel resultModel = new ResultModel();
//        UserDao userDao = new UserImpl();
//        List<UserModel> list = userDao.findAll(userModel);
//        resultModel.setData(list);
//        resultModel.setCode(0);
//        int count = userDao.getCount(userModel);
//        resultModel.setCount(count);
        return ResultModel.getModel(userDao.findAll(userModel), userDao.getCount(userModel));
    }

    @Override
    public ResultModel updateEnable(UserModel userModel) {
//        ResultModel resultModel = new ResultModel();
//        UserDao userDao = new UserImpl();
        int result = userDao.updateEnable(userModel);
//        resultModel.setMsg(result == 1 ? "success" : "error");
        return ResultModel.getModel(result == 1 ? "success" : "error");
    }

    @Override
    public ResultModel updateField(UserModel userModel) {
//        ResultModel resultModel = new ResultModel();
//        UserDao userDao = new UserImpl();
        if (userModel.getField().equals("username")) {
            UserModel model = userDao.findByUsername(userModel);
            if (model.getUsername().equals(userModel.getValue())) {
//                resultModel.setMsg("sameUsername");
                return ResultModel.getModel("sameUsername");
            }
        }
        int result = userDao.updateField(userModel);
//        resultModel.setMsg(result == 1 ? "success" : "error");
//        return resultModel;
        return ResultModel.getModel(result == 1 ? "success" : "error");
    }

    @Override
    public ResultModel delete(UserModel userModel) {
//        ResultModel resultModel = new ResultModel();
//        UserDao userDao = new UserImpl();
        int result = userDao.delete(userModel);
//        resultModel.setMsg(result == 1 ? "success" : "error");
        return ResultModel.getModel(result == 1 ? "success" : "error");
    }

    @Override
    public ResultModel add(UserModel userModel) {
//        ResultModel resultModel = new ResultModel();
//        UserDao userDao = new UserImpl();
        int result = userDao.add(userModel);
//        resultModel.setMsg(result == 1 ? "success" : "error");
        return ResultModel.getModel(result);
    }

    @Override
    public ResultModel findById(UserModel userModel) {
//        ResultModel resultModel = new ResultModel();
//        UserDao userDao = new UserImpl();
//        UserModel model = userDao.findById(userModel);
//        resultModel.setData(model);
        return ResultModel.getModel(userDao.findById(userModel));
    }

    @Override
    public ResultModel update(UserModel userModel) {
        return ResultModel.getModel(userDao.update(userModel));
    }

    @Override
    public ResultModel getCount(UserModel userModel) {
        return null;
    }
}
